package senior.MapTest.exer3;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @author shkstart
 * @creat 2022-04-15:28
 */
public class Firm {
    private String firmName;
    private String phone;
    private String address;
    private String scope;
    private HashMap<String, Employee> map = new HashMap<>();

    public Firm() {
    }

    public Firm(String firmName, String phone, String address, String scope) {
        this.firmName = firmName;
        this.phone = phone;
        this.address = address;
        this.scope = scope;
    }

    public String getFirmName() {
        return firmName;
    }

    public void setFirmName(String firmName) {
        this.firmName = firmName;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getScope() {
        return scope;
    }

    public void setScope(String scope) {
        this.scope = scope;
    }

    public HashMap<String, Employee> getMap() {
        return map;
    }

    public void setMap(HashMap<String, Employee> map) {
        this.map = map;
    }

    @Override
    public String toString() {
        return  "\t\t\t公司名称：" + firmName +
                "\n\t\t\t联系电话：" + phone +
                "\n\t\t\t地址：" + address +
                "\n\t\t\t经营范围：" + scope;
    }

    /**
     * 添加员工
     *
     * @param no
     * @param employee
     * @return
     */
    public boolean addEmploy(String no, Employee employee) {
        if (map.containsKey(no)) {
            return false;
        }
        map.put(no, employee);
        return true;
    }

    /**
     * 根据工号开除员工
     *
     * @param no
     * @return
     */
    public boolean deleteEmploy(String no) {
        Set<Map.Entry<String, Employee>> entries = map.entrySet();
        Iterator<Map.Entry<String, Employee>> iterator = entries.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Employee> next = iterator.next();
            String key = next.getKey();
            if (key.equals(no)) {
                iterator.remove();
                return true;
            }
        }
        return false;

    }

    /**
     * 展示员工信息
     */
    public void showEmploy() {
        System.out.println("共有" + map.size() + "名员工：");
        Set<Map.Entry<String, Employee>> entries = map.entrySet();
        Iterator<Map.Entry<String, Employee>> iterator = entries.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Employee> next = iterator.next();
            String no = next.getKey();
            Employee employee = next.getValue();
            System.out.println("工号：" + no + "," + employee);

        }
    }
}
